﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using CoreInmobiliario.Data;

namespace CoreInmobiliario.Business
{
    [DataObject]
    public class Servicio
    {
        public static Servicio GetInstance()
        {
            return new Servicio();
        }

        private static DBDataContext GetDBDataContext()
        {
            Factories.DBDataContextFactory factory = new Factories.DBDataContextFactory();
            return factory.CreateDataContext() as DBDataContext;
        }

        public Data.Servicio GetById(int servicioId)
        {
            Data.Servicio servicio = null;

            using (DBDataContext dataContext = GetDBDataContext())
            {
                try
                {
                    servicio = dataContext.Servicios.FirstOrDefault(s => s.ServicioId.Equals(servicioId));
                }
                catch (Exception ex)
                {

                }
            }
            return servicio;
        }

        public List<Data.Servicio> GetList()
        {
            List<Data.Servicio> list = null;

            using (DBDataContext dataContext = GetDBDataContext())
            {
                try
                {
                    list = dataContext.Servicios.ToList();

                }
                catch (Exception ex)
                {

                }
            }
            return list;
        }

        public List<Data.Servicio> GetListServicioByPropiedadId(int propiedadId)
        {
            List<Data.Servicio> list = null;

            using (DBDataContext dc = GetDBDataContext())
            {
                try
                {
                    var query = from p in dc.GetTable<Data.Propiedad>()
                                from ps in dc.GetTable<Data.PropiedadServicio>()
                                from s in dc.GetTable<Data.Servicio>()
                                where
                                    (ps.PropiedadId == p.PropiedadId) &&
                                    (p.PropiedadId == propiedadId) &&
                                    (s.ServicioId == ps.ServicioId)
                                select s;

                    list = query.ToList();
                }
                catch (Exception ex)
                {

                }
            }

            return list;
        }
    }
}
